import Vue from 'vue'
import VueRouter from 'vue-router'
// 在router.js 中获取 vuex 中的值
import store from '@/store'
import {
  setItem
} from '@/utils/storage'

Vue.use(VueRouter)

const routes = [{ // 如果父路由有默认子路由，那它的 name 没有意义
    path: '/',
    name: 'home',
    component: () => import('@/components/Home'),
    children: [{
        path: '/swiperDemo',
        name: 'swiperDemo',
        component: () => import('@/module-hweye/page/SwiperDemo.vue')
      },
      {
        path: '/lessDemo',
        name: 'lessDemo',
        component: () => import('@/module-hweye/page/LessDemo')
      },
      {
        path: '/routerDemo',
        name: 'routerDemo',
        component: () => import('@/module-hweye/page/RouterDemo'),
        // 组件内置路由
        beforeEnter(to, from, next) {
          if (Math.random() > 0.3) {
            next()
          } else {
            alert('random 小于 0.3咯, ❤❤❤❤❤❤❤❤❤❤❤')
          }
        }
      },
      {
        path: '/dataHijacking',
        name: 'dataHijacking',
        component: () => import('@/module-hweye/page/dataHijacking')
      },
      {
        path: '/eveceduty',
        component: () => import('@/module-hweye/page/SwiperDemo')
      },
      {
        path: '/cacheDemo',
        component: () => import('@/module-cmdb/cacheDemo')
      },
      {
        path: '/subMentItem',
        component: () => import('@/module-cmdb/subMentItem.vue')
      },
    ]
  },
  {
    path: '/auto',
    component: () => import('@/module-auto/auto')
  }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.afterEach((to, from, next) => {
  const {
    collectObj
  } = store.state
  //     next()  放行    next('/login')  强制跳转
  if (to.path === '/login') return next()

  const {
    fullPath
  } = to
  // console.log('fullPath===>', fullPath)
  if (fullPath.includes('/swiperDemo')) {
    collectObj.swiper += 1
  } else if (fullPath.includes('/lessDemo')) {
    collectObj.less += 1
  } else if (fullPath.includes('/routerDemo')) {
    collectObj.router += 1
  }
  setItem('COLLECT_OBJ', collectObj)
  store.commit('CHANGE_STATE', {
    key: 'collectObj',
    value: collectObj
  })
  // next()
})

export default router
